package com.sw.bbs.common.advice;

import com.sw.bbs.common.common.GlobalException;
import com.sw.bbs.common.vo.RestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * RestExceptionWapper
 *
 * @author Johnathan
 */
@ControllerAdvice("com.sw.bbs")
public class RestExceptionWapper {

    private Logger logger = LoggerFactory.getLogger(RestExceptionWapper.class);

    @ExceptionHandler(value = {GlobalException.class})
    public ResponseEntity<?> errorHandler(GlobalException e) {
        RestResponse exResoponse = new RestResponse();
        exResoponse.setErrorMsg(e.getErrMsg());
        exResoponse.setErrorCode(String.valueOf(e.getErrCode()));

        logger.error("request api:{} append a exception.", e.getUri(), e);

        return new ResponseEntity<Object>(exResoponse, HttpStatus.OK);
    }
}
